import React from 'react';
import { Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message, Steps } from 'antd';

import OperatorConstant from "../../utils/OperatorConstant";
import Inoviceheader from '../../components/drawback/Inoviceheader'
import Inoviceproducts from '../../components/drawback/Inoviceproducts';
import AddInvoice from '../../components/drawback/AddInvoice';
import moment from 'moment'
const { TextArea } = Input;
const Step = Steps.Step;
const Dragger = Upload.Dragger;
const R = require('ramda');
import Constants from '../../utils/Constants'
const TabPane = Tabs.TabPane;
const FormItem = Form.Item;
const dateFormat = 'YYYY-MM-DD';




export default Form.create({ wrappedComponentRef: true })(class OpeninvoiceDetail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: true,
            type: "",
            tabActiveKey: "1",
            disabled: false,
            orderId: "",
            openinvoicestatus: "",

        }
    }



    handleResetFields = () => {
        if (this.inoviceheader) { this.inoviceheader.handleCancel() }
        if (this.inoviceproducts) { this.inoviceproducts.handleCancel() }
        if (this.addInvoice) { this.addInvoice.handleCancel() }

        this.props.form.resetFields();

    }

    handleCancel = () => {
        this.handleResetFields();
        this.setState({ open: false, orderId: "", openinvoicestatus: "", disabled: false });
    }

    handleOpen = (type, orderId, openinvoicestatus, disabled) => {
        this.state.type = type;
        this.setState({ open: true, orderId: orderId, openinvoicestatus: openinvoicestatus, disabled: disabled });
    }

    handleConfirm = () => {
        if (this.state.type == OperatorConstant.view) {
            return;
        }

        let values = this.props.form.getFieldsValue();

        let list = R.isNil(this.props.inoviceproductsList) || R.isEmpty(this.props.inoviceproductsList) ? [] : this.props.inoviceproductsList;
        if (!R.isEmpty(list)) {
            for (let i in list) {
                if (list[i].exchangecost >= Constants.EXCHANGE_COST_LOWER_LIMIT || list[i].exchangecost <= Constants.EXCHANGE_COST_UPPER_LIMIT) {
                    message.error("换汇成本应当在" + Constants.EXCHANGE_COST_UPPER_LIMIT + "~" + Constants.EXCHANGE_COST_LOWER_LIMIT + "之间");
                    return
                }
                if (!list[i].zzrate) { message.error(`序号为${list[i].id}的产品增值税率不能为空`); return; }
                if (!list[i].taxrate) { message.error(`序号为${list[i].id}的产品退税率不能为空`); return; }
            }

        }

        this.props.form.validateFields((errors, values) => {
            if (errors) return;

            let dataSource = R.isNil(this.props.invoices) || R.isEmpty(this.props.invoices) ? {} : this.props.invoices[0];


            if (this.state.type == OperatorConstant.openInvoice) {
                this.props.dispatch({
                    type: 'invoice/openinvoice',
                    payload: {
                        orderId: dataSource.orderId,
                        list: list,
                        onSuccess: () => {
                            //下载开票资料
                            this.props.dispatch({ type: 'invoice/exportInvoice', payload: { orderId: this.state.orderId, drawerId: list[0].drawerid } });
                            this.props.handleSearch();
                            this.handleCancel();
                        }
                    }
                });

            }
        });
    }




    handleCollectForm = () => {
        this.form.payOrder = this.refs.payOrder.getUrl();//this.state.orderso;
        return this.form;
    }

    render() {
        let otherFieldValues = this.props.form.getFieldsValue();
        let invoices = R.isNil(this.props.invoices) ? {} : this.props.invoices;
        let inoviceproductsList = R.isNil(this.props.inoviceproductsList) ? [] : this.props.inoviceproductsList;
        let totalprice = R.isNil(this.props.totalprice) ? 0 : this.props.totalprice;


        const { getFieldDecorator } = this.props.form;
        const formItemLayout = { labelCol: { xs: { span: 8 }, sm: { span: 5 } }, wrapperCol: { xs: { span: 15 }, sm: { span: 14 } } };


        let footer = [];

        if (!this.state.disabled) {
            footer.push(<Button key="apply" size="large" onClick={this.handleConfirm.bind(this)}>做开票资料</Button>);

        }

        footer.push(<Button key="cancel" size="large" onClick={this.handleCancel.bind(this)}>取消</Button>);

        let disable = (this.state.type == OperatorConstant.view ? true : false);

        return (
            <Modal maskClosable={false} visible={this.state.open} title={"做开票资料"} width={1000} wrapClassName="vertical-center-modal"
                onCancel={this.handleCancel.bind(this)} footer={footer}>

                <div>
                    <Inoviceheader wrappedComponentRef={(inst) => this.inoviceheader = inst} dispatch={this.props.dispatch}
                        totalprice={totalprice}
                        dataSource={invoices} />

                    <Inoviceproducts wrappedComponentRef={(inst) => this.inoviceproducts = inst} dispatch={this.props.dispatch}
                        type={this.state.type}
                        openinvoicestatus={this.state.openinvoicestatus}
                        disabled={this.state.disabled}
                        // totalprice = {totalprice}
                        dataSource={inoviceproductsList} />

                </div>



            </Modal>
        );
    }
})


